package com.sdk.es.sdk.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * @author limaosheng
 * @create 2024-04-03 11:20
 * @desc
 */
public class EsSet {
    public static Long toL(Integer val) {
        if(val == null){
            return null;
        }
        return val.longValue();
    }
    public static String toS(Integer val) {
        if(val == null){
            return null;
        }
        return val.toString();
    }
    public static Integer toI(Long val) {
        if(val == null){
            return null;
        }
        return val.intValue();
    }
    public static String toS(Long val) {
        if(val == null){
            return null;
        }
        return val.toString();
    }
    public static Integer toI(String val) {
        if(EsStrUtils.isBlank(val)){
            return null;
        }
        try {
            return Integer.parseInt(val);
        } catch (Exception e) {
            return null;
        }
    }
    public static Long toL(String val) {
        if(EsStrUtils.isBlank(val)){
            return null;
        }
        try {
            return Long.parseLong(val);
        } catch (Exception e) {
            return null;
        }
    }
    public static LocalDateTime toLDT(Long val) {
        if(val == null){
            return null;
        }
        try {
            // 将时间戳转换为Instant对象
            Instant instant = Instant.ofEpochMilli(val);
            // 转换Instant对象到默认区域的LocalDateTime
            return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        } catch (Exception e) {
            return null;
        }
    }
    public static Long toL(LocalDateTime val) {
        if(val == null){
            return null;
        }
        return val.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000; // 转换为毫秒时间戳
    }
    public static Float toF(Double val) {
        if(val == null){
            return null;
        }
        return val.floatValue();
    }
    public static LocalDateTime toLDT(LocalDate val) {
        if(val == null){
            return null;
        }
        return val.atStartOfDay();
    }
}
